<template>
  <div>
    <el-table class="table_el" :data="userlist" style="width: auto">
      <el-table-column label="ID" prop="user_id" width="60" />
      <el-table-column label="用户名" prop="username" />
      <el-table-column width="200">
        <template #header>
          <el-input v-model="userPage.key" size="small" placeholder="关键字筛选" style="width: 120px" @blur="findIt" />
        </template>
        <template #default="scope">
          <el-button size="small" @click="outerVisible(scope.row.user_id)">
            确认
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="flex-c m-t-20px">
      <el-pagination background layout="prev, pager, next,sizes" :total="total" :current-page="userPage.page"
        :page-size="userPage.count" :page-sizes="[10, 20, 50, 100]" @update:current-page="handleCurrentPageChange"
        @update:page-size="handlePageSizeChange" />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive, defineEmits } from "vue";
import { getUserList, getUserInfoV1 } from "@/api/user";
import { mainUrl } from "@/api/utils";

const emit = defineEmits(["SetIdUSer"]);
const userlist = ref([]);
const userPage = reactive({
  page: 1,
  count: 10,
  key: ""
});
onMounted(async () => {
  await getList(userPage);
  await getAvatar();
});

const getList = async data => {
  await getUserList(data).then(res => {
    userlist.value = res.data.data;
    total.value = res.data.count;
  });
};
const total = ref(10);
const handlePageSizeChange = async newSize => {
  userPage.count = newSize;
  getList(userPage);
};
//分页
const handleCurrentPageChange = async newPage => {
  userPage.page = newPage;
  getList(userPage);
};

const vipuser = ref(false);
const avatar = ref();
const getAvatar = async () => {
  let userRes = await getUserInfoV1();
  let userInfo = userRes.data.data;
  if (userRes.data.data.user_type == 1) {
    vipuser.value = true;
  }
  if (userInfo.avatar != null) {
    avatar.value = `${mainUrl}/v1/attachment/get_attachment?aid=${userInfo.avatar}`;
  }
};

const outerVisible = id => {
  emit("SetIdUSer", id);
};
const findIt = text => {
  getList(userPage);
};
</script>

<style scoped></style>
